package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.MessageInfo;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageDetailActivity extends BaseActivity {
    private TextView ic_shiping_order_num, tv_message, ic_senduser1,ic_senduser2,ic_time1,ic_time2;
    private Intent mIntent;
    private MessageInfo messageInfo;
    private ProgressDialog mProgressDialog;
    private Context mContext;
    private Toolbar toolbar;
    //0、1为接单，2已接单，3电子围栏签收，4实际签收,5分配订单
    private  String state = "1";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_message_detail);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ic_shiping_order_num = (TextView) findViewById(R.id.ic_shiping_order_num).findViewById(R.id.tv_name);
        ic_senduser1 = (TextView) findViewById(R.id.ic_senduser).findViewById(R.id.tv_left);
        ic_senduser2 = (TextView) findViewById(R.id.ic_senduser).findViewById(R.id.tv_right);
        ic_time1 = (TextView) findViewById(R.id.ic_time).findViewById(R.id.tv_left);
        ic_time2 = (TextView) findViewById(R.id.ic_time).findViewById(R.id.tv_right);
        tv_message = (TextView) findViewById(R.id.tv_message);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("消息详情");
        setSupportActionBar(toolbar);
        mIntent = getIntent();
        messageInfo = (MessageInfo) mIntent.getExtras().get(
                "messageInfo");
        ic_senduser1.setText("发送人：");
        ic_time1.setText("发送时间：");
        state = messageInfo.getOrderMsgState();
        String messageid = messageInfo.getOrderMsgId();
        if (state.equals("1")){
            ReceiveOrderTask showOrderTask = new ReceiveOrderTask();
            showOrderTask.execute(messageid);
        }
        setData(messageInfo);
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new mOnClick());

    }

    private void setData(MessageInfo messageInfo) {
        ic_shiping_order_num.setText(messageInfo.getOrderMsgTitle());
        tv_message.setText( messageInfo.getOrderMsg());
        ic_time2.setText(messageInfo.getUpdatetime());
        ic_senduser2.setText( messageInfo.getSendUserName());


    }
    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                default:
                    finish();
                    break;
            }
        }
    }

    public class ReceiveOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("state","2");
            map.put("messageId", params[0]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appUpdateMsgState"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");

                }
            }
        }

        @Override
        protected void onCancelled() {

        }
    }
}
